using UnityEngine;
using UnityEngine.InputSystem;

namespace SunshineFarmWord
{
    public class PlayerWalkState : PlayerBaseStates
    {
        public PlayerWalkState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
        }

        #region base function
        public override void Enter()
        {
            base.Enter();

            InitReusableData();

            StartAnimation(stateMachine.Player.AnimationData.WalkParameterHash);
        }

        public override void Exit()
        {
            base.Exit();

            ClearReusableData();

            StopAnimation(stateMachine.Player.AnimationData.WalkParameterHash);
        }

        public override void PhysicsUpdate()
        {
            base.PhysicsUpdate();

            Move();
        }
        #endregion

        #region Main function

        //移动
        protected override void Move()
        {
            // 内部处理了移动方法 这里判断是否需要移动即可
            if (!stateMachine.ReusableData.IsWalkState) return;
            base.Move();
        }

        // 初始化数据
        private void InitReusableData()
        {
            stateMachine.ReusableData.IsWalkState = true;
            stateMachine.ReusableData.IsRunState = false;
            stateMachine.ReusableData.MovementSpeedModifier = movementData.WalkBaseSpeed;
        }

        // 退出时清空数据
        private void ClearReusableData()
        {
            stateMachine.ReusableData.IsWalkState = false;
            stateMachine.ReusableData.MovementSpeedModifier = 0f;
        }
        #endregion


        #region 输入回调
        // 取消移动
        protected override void OnMovementCanceled(InputAction.CallbackContext context)
        {
            base.OnMovementCanceled(context);
            //Debug.Log("取消移动-----");
            stateMachine.ChangeState(stateMachine.IdlingState);
        }

        // 移动中
        protected override void OnMovementPerformed(InputAction.CallbackContext context)
        {
            base.OnMovementPerformed(context);
            //Debug.Log("移动-----");
        }

        // 开始移动
        protected override void OnMovementStarted(InputAction.CallbackContext context)
        {
            base.OnMovementStarted(context);
            //Debug.Log("开始移动-----");
        }

        // shift 事件
        protected override void OnAccelerateStarted(InputAction.CallbackContext context)
        {
            base.OnAccelerateStarted(context);

            stateMachine.ReusableData.IsRunState = !stateMachine.ReusableData.IsRunState;

            if (stateMachine.ReusableData.IsRunState)
            {
                stateMachine.ChangeState(stateMachine.RunState);
            }
        }
        #endregion
    }
}
